package com.it.xuehang.controller;

import com.it.xuehang.entity.ResourceData;
import com.it.xuehang.exception.ValidationFailedException;
import com.it.xuehang.service.ResourceDataService;
import com.it.xuehang.util.GuidUtils;
import com.it.xuehang.util.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 资源数据 前端控制器
 * </p>
 *
 * @author xuehang
 * @since 2024-12-10
 */
@RestController
@RequestMapping("/resourceData")
@Slf4j
public class ResourceDataController {
    @Autowired
    private ResourceDataService resourceDataService;
    /**
     * 新增
     */
    @PostMapping("/add")
   public ResponseResult<Void> addResourceData(
            @RequestBody @Valid ResourceData resourceData, BindingResult result){
        log.info("resourceData----->{}",resourceData);
        if(result.hasErrors()){
            //表单不符合验证规则时抛出自定义异常
            throw  new ValidationFailedException(result);
        }
        resourceData.setResourceid(GuidUtils.generateGUID());
        resourceDataService.addResourceData(resourceData);
       return new ResponseResult<>(200,"success");
   }
    /**
     * 删除
     */
    @GetMapping("/del")
    public ResponseResult<Void> delResourceData(String resourceid){
        resourceDataService.delResourceData(resourceid);
        return new ResponseResult<>(200,"success");
    }
    /**
     * 修改
     */
    @PostMapping("/update")
    public ResponseResult<Void> updateResourceData(
            @RequestBody @Valid ResourceData resourceData,BindingResult result){
        if(result.hasErrors()){
            //表单不符合验证规则时抛出自定义异常
            throw  new ValidationFailedException(result);
        }
        log.info("resourceData----->{}",resourceData);
        resourceDataService.updateResourceData(resourceData);
        return new ResponseResult<>(200,"success");
    }
    /**
     * 查询
     */
    @GetMapping("/query")
    public ResponseResult<List<ResourceData>> queryResourceData( @RequestParam(required = false) String keyword,
                                                   @RequestParam(required = false)
                                                   @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate,
                                                   @RequestParam(required = false)
                                                   @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate,
                                                   @RequestParam(required = false) String rescateid){
        List<ResourceData> resourceDataList = resourceDataService.selectListByCondition(keyword, startDate, endDate, rescateid);
        return new ResponseResult<>(200,resourceDataList);
    }


    /**
     * 根据中心点经纬度坐标和半径查询圆形范围内的资源数据。
     *
     * @param centerLat 中心点纬度
     * @param centerLng 中心点经度
     * @param radius    半径（单位：公里）
     * @return 查询结果列表
     */
    @GetMapping("/search/circle")
    public ResponseResult<List<ResourceData>> searchWithinCircle(
            @RequestParam Double centerLat,
            @RequestParam Double centerLng,
            @RequestParam Double radius) {
        return new ResponseResult<>(200,
                resourceDataService.searchWithinCircle(centerLat, centerLng, radius));
    }

    @PostMapping("/import-excel")
    public Map<String, Object> importExcel(@RequestParam("file") MultipartFile file) {
        log.info("file:{}",file);
        return  resourceDataService.importResources(file);
    }

    @GetMapping("/export-excel")
    public void exportExcel(HttpServletResponse response) {
        resourceDataService.exportResourcesToExcel(response);
    }


}
